Page({
  data: {
    consultantList: [], // 存储咨询师列表数据
  },
  onLoad() {
    // 页面加载时调用加载咨询师列表的方法
    this.loadConsultants(); 
  },
  loadConsultants() {
    wx.request({
      url: 'http://localhost:8080/consultant1s/listAll', 
      method: 'GET',
      // 成功回调
      success: (res) => {
        if (res.data) {
          this.setData({
            consultantList: res.data, // 将响应数据设置到consultantList
          });
        }
      },
      // 失败回调
      fail: (err) => {
        console.error('请求失败:', err);
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        });
      }
    });
  },
  deleteConsultant(e) {
    const consultantId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认删除',
      content: '确定要删除该咨询师吗？',
      success: (res) => {
        if (res.confirm) {
          wx.request({
            url: `http://localhost:8080/consultant1s/delete/${consultantId}`,
            method: 'DELETE',
            success: () => {
              wx.showToast({
                title: '删除成功',
                icon: 'success'
              });
              this.loadConsultants(); 
            },
            fail: (err) => {
              console.error('删除失败:', err);
              wx.showToast({
                title: '删除失败',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  },
  submitForm(e) {
    const formData = e.detail.value;
    wx.request({
      url: 'http://localhost:8080/consultant1s/add',
      method: 'POST',
      data: formData,
      success: () => {
        wx.showToast({
          title: '新增成功',
          icon: 'success'
        });
        this.loadConsultants(); // 重新加载咨询师列表
      },
      fail: (err) => {
        console.error('新增失败:', err);
        wx.showToast({
          title: '新增失败',
          icon: 'none'
        });
      }
    });
  },
  onConsultantTap(e) {
    const consultantId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/zixun/zixunshixiangqing/zixunshixiangqing?id=${consultantId}`,
    });
  },
});